package com.example.sunny.view.weather

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import com.baidu.location.LocationClient
import com.example.sunny.SunnyApplication
import com.example.sunny.service.Repository
import com.example.sunny.pojo.place.Location
import com.example.sunny.pojo.location.MyLocation
import com.example.sunny.pojo.weather.Weather
import com.example.sunny.utils.NetUtils

class WeatherViewModel:ViewModel() {

    val permissionList = listOf<String>(
        android.Manifest.permission.ACCESS_NETWORK_STATE,
        android.Manifest.permission.READ_SYNC_SETTINGS,
        android.Manifest.permission.WAKE_LOCK,
        android.Manifest.permission.CHANGE_WIFI_STATE,
        android.Manifest.permission.ACCESS_WIFI_STATE,
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
        android.Manifest.permission.READ_EXTERNAL_STORAGE,
        android.Manifest.permission.ACCESS_COARSE_LOCATION,
        android.Manifest.permission.ACCESS_FINE_LOCATION,
        android.Manifest.permission.READ_PHONE_STATE,
        android.Manifest.permission.SCHEDULE_EXACT_ALARM,
        android.Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
        android.Manifest.permission.SET_ALARM,

        )

    var isShowing =false

    var locationClient:LocationClient?=null

    var isCentigrade = MutableLiveData<Boolean>()

    private val locationLiveData = MutableLiveData<Location>()

    var isNetConnected = MutableLiveData<Boolean>()

    var myLocation: MyLocation? = null
    var weather:Weather? = null
    var weatherLiveData = Transformations.switchMap(locationLiveData){location->
        Repository.refreshWeather(location.lng,location.lat,SunnyApplication.context)
    }

    init {
        isCentigrade.value = true
    }
    fun refreshWeather(){
        refreshLocation()
        Log.d("WeatherViewModel",myLocation.toString())
        locationLiveData.value = Location(myLocation?.lng.toString(), myLocation?.lat.toString())
    }

    fun refreshLocation(){
        if (!NetUtils.isNetWorkConnected(SunnyApplication.context)){

            val refreshLocation = Repository.refreshLocation()
            this.myLocation = refreshLocation
        }
    }
}